<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Registration extends MX_Controller
{
    function __construct()
    {
        parent::__construct();
        
        $this->properti = $this->property->get();
        $this->acl->otentikasi();

        $this->modul = $this->components->get(strtolower(get_class($this)));
        $this->title = strtolower(get_class($this));

        $this->currency   = $this->load->library('currency_lib');
        $this->user       = $this->load->library('admin_lib');
        $this->journal    = $this->load->library('journal_lib');
        $this->journalgl  = $this->load->library('journalgl_lib');
        $this->dept       = $this->load->library('dept_lib');
        $this->faculty    = new Faculty_lib();
        $this->financial  = new Financial_lib(); 
        $this->student    = new Student_lib();
        $this->payment    = new Payment_type_lib(); 
        $this->uniform    = new Uniform_lib();
        
        $this->model = new Registrations();

        $this->load->library('fusioncharts');
        $this->swfCharts  = base_url().'public/flash/Column3D.swf';

    }

    private $properti, $modul, $title, $currency,$model,$payment;
    private $user,$journal,$dept,$faculty,$financial, $student, $uniform;

    function index()
    {
        $this->get_last();
    }

    function get_last()
    {
        $this->acl->otentikasi1($this->title);

        $data['title'] = $this->properti['name'].' | Administrator  '.ucwords($this->modul['title']);
        $data['h2title'] = $this->modul['title'];
        $data['main_view'] = 'registration_view';
	$data['form_action'] = site_url($this->title.'/search');
        $data['form_action_graph'] = site_url($this->title.'/get_last');
        $data['link'] = array('link_back' => anchor('main/','<span>back</span>', array('class' => 'back')));

        $data['currency'] = $this->currency->combo();
        $data['dept'] = $this->dept->combo_all();
        $data['year'] = $this->financial->combo_active(); 
        
	$uri_segment = 3;
        $offset = $this->uri->segment($uri_segment);

	// ---------------------------------------- //
        $saless = $this->model->get($this->modul['limit'], $offset);
        $num_rows = $this->model->count();

        $atts = array('width'=> '450','height'=> '300',
                      'scrollbars' => 'yes','status'=> 'yes',
                      'resizable'=> 'yes','screenx'=> '0','screenx' => '\'+((parseInt(screen.width) - 400)/2)+\'',
                      'screeny'=> '0','class'=> 'print','title'=> 'print', 'screeny' => '\'+((parseInt(screen.height) - 200)/2)+\'');
        
        $atts1 = array('width'=> '800','height'=> '600',
                      'scrollbars' => 'yes','status'=> 'yes',
                      'resizable'=> 'yes','screenx'=> '0','screenx' => '\'+((parseInt(screen.width) - 800)/2)+\'',
                      'screeny'=> '0','class'=> 'details1','title'=> 'update', 'screeny' => '\'+((parseInt(screen.height) - 600)/2)+\'');

        if ($num_rows > 0)
        {
	    $config['base_url'] = site_url($this->title.'/get_last');
            $config['total_rows'] = $num_rows;
            $config['per_page'] = $this->modul['limit'];
            $config['uri_segment'] = $uri_segment;
            $this->pagination->initialize($config);
            $data['pagination'] = $this->pagination->create_links(); //array menampilkan link untuk pagination.
            // akhir dari config untuk pagination
            

            // library HTML table untuk membuat template table class zebra
            $tmpl = array('table_open' => '<table cellpadding="2" cellspacing="1" class="tablemaster">');

            $this->table->set_template($tmpl);
            $this->table->set_empty("&nbsp;");

            //Set heading untuk table
            $this->table->set_heading('No', 'Code', 'Cur', 'Date', 'Department', 'Student', 'Balance', 'Status', 'Paid', 'Action');

            $i = 0 + $offset;
            foreach ($saless as $sales)
            {
                $datax = array('name'=> 'cek[]','id'=> 'cek'.$i,'value'=> $sales->id,'checked'=> FALSE, 'style'=> 'margin:0px');
                
                $this->table->add_row
                (
                    ++$i, 'REG-0'.$sales->id, $sales->currency, tglin($sales->dates), $this->dept->get_name($sales->dept_id), $this->student->get_name($sales->student_id), number_format($sales->total), $sales->payment_status, $sales->paid_status,
                    anchor_popup($this->title.'/invoice/'.$sales->no,'<span>print</span>',$atts).' '.
                    anchor_popup($this->title.'/update/'.$sales->no,'<span>update</span>',$atts1).' '.
                    anchor($this->title.'/delete/'.$sales->id.'/'.$sales->no,'<span>delete</span>',array('class'=> 'delete', 'title' => 'delete' ,'onclick'=>"return confirm('Are you sure you will delete this data?')"))
                );
            }

            $data['table'] = $this->table->generate();
        }
        else
        {
            $data['message'] = "No $this->title data was found!";
        }

        // ===== chart  =======
       // $data['graph'] = $this->chart($this->input->post('ccurrency'));
        

        // Load absen view dengan melewatkan var $data sbgai parameter
	$this->load->view('template', $data);
    }


    private function chart($cur='IDR')
    {
        $ps = new Period();
        $ps->get();
        $year = $ps->year;

        $arpData[0][1] = 'January';
        $arpData[0][2] = $this->tm->total_chart('01',$year,$cur);

        $arpData[1][1] = 'February';
        $arpData[1][2] = $this->tm->total_chart('02',$year,$cur);

        $arpData[2][1] = 'March';
        $arpData[2][2] = $this->tm->total_chart('03',$year,$cur);

        $arpData[3][1] = 'April';
        $arpData[3][2] = $this->tm->total_chart('04',$year,$cur);

        $arpData[4][1] = 'May';
        $arpData[4][2] = $this->tm->total_chart('05',$year,$cur);

        $arpData[5][1] = 'June';
        $arpData[5][2] = $this->tm->total_chart('06',$year,$cur);

        $arpData[6][1] = 'July';
        $arpData[6][2] = $this->tm->total_chart('07',$year,$cur);

        $arpData[7][1] = 'August';
        $arpData[7][2] = $this->tm->total_chart('08',$year,$cur);

        $arpData[8][1] = 'September';
        $arpData[8][2] = $this->tm->total_chart('09',$year,$cur);

        $arpData[9][1] = 'October';
        $arpData[9][2] = $this->tm->total_chart('10',$year,$cur);

        $arpData[10][1] = 'November';
        $arpData[10][2] = $this->tm->total_chart('11',$year,$cur);

        $arpData[11][1] = 'December';
        $arpData[11][2] = $this->tm->total_chart('12',$year,$cur);

        $strXML1        = $this->fusioncharts->setDataXML($arpData,'','') ;
        $graph = $this->fusioncharts->renderChart($this->swfCharts,'',$strXML1,"Tuition", "98%", 400, false, false) ;
        return $graph;
    }

    function search()
    {
        $this->acl->otentikasi1($this->title);

        $data['title'] = $this->properti['name'].' | Administrator Find '.ucwords($this->modul['title']);
        $data['h2title'] = 'Find '.$this->modul['title'];
        $data['main_view'] = 'registration_view';
	$data['form_action'] = site_url($this->title.'/search');
        $data['link'] = array('link_back' => anchor($this->title,'<span>back</span>', array('class' => 'back')));

        $data['currency'] = $this->currency->combo();

        $atts = array('width'=> '400','height'=> '220',
                      'scrollbars' => 'yes','status'=> 'yes',
                      'resizable'=> 'yes','screenx'=> '0','screenx' => '\'+((parseInt(screen.width) - 400)/2)+\'',
                      'screeny'=> '0','class'=> 'print','title'=> 'print', 'screeny' => '\'+((parseInt(screen.height) - 200)/2)+\'');

        if ($this->input->post('tdate')){ $saless = $this->model->where('dates', $this->input->post('tdate'))->get(); }
        else { $saless = $this->model->get(); }
        
        $tmpl = array('table_open' => '<table cellpadding="2" cellspacing="1" class="tablemaster">');

        $this->table->set_template($tmpl);
        $this->table->set_empty("&nbsp;");

        //Set heading untuk table
        $this->table->set_heading('No', 'Code', 'Cur', 'Date', 'Notes', 'Balance', 'Action');

        $i = 0;
        foreach ($saless as $sales)
        {
            $datax = array('name'=> 'cek[]','id'=> 'cek'.$i,'value'=> $sales->id,'checked'=> FALSE, 'style'=> 'margin:0px');

            $this->table->add_row
            (
                ++$i, 'TJ-00'.$sales->no, $sales->currency, tglin($sales->dates), ucfirst($sales->notes), number_format($sales->total),
                anchor($this->title.'/confirmation/'.$sales->id,'<span>update</span>',array('class' => $this->post_status($sales->approved), 'title' => 'edit / update')).' '.
                anchor_popup($this->title.'/invoice/'.$sales->no,'<span>print</span>',$atts).' '.
                anchor($this->title.'/delete/'.$sales->id.'/'.$sales->no,'<span>delete</span>',array('class'=> 'delete', 'title' => 'delete' ,'onclick'=>"return confirm('Are you sure you will delete this data?')"))
            );
        }

        $data['table'] = $this->table->generate();
        $this->load->view('template', $data);
    }

    function add()
    {
//        $this->acl->otentikasi2($this->title);

        $data['title'] = $this->properti['name'].' | Administrator  '.ucwords($this->modul['title']);
        $data['h2title'] = $this->modul['title'];
        $data['main_view'] = 'regform';
	$data['form_action'] = site_url($this->title.'/add_process');
	$data['link'] = array('link_back' => anchor($this->title,'<span>back</span>', array('class' => 'back')));
        
        $data['dept'] = $this->dept->combo_all();
        $data['faculty'] = $this->faculty->combo();
        $data['payment'] = $this->payment->combo();
        
        $this->load->view('reg_form', $data);
    }
    
    function add_process()
    {
        $this->acl->otentikasi2($this->title);

        $data['title'] = $this->properti['name'].' | Administrator  '.ucwords($this->modul['title']);
        $data['h2title'] = $this->modul['title'];
	$data['form_action'] = site_url($this->title.'/add_process');
         
        $data['dept'] = $this->dept->combo_all(); 
        $data['faculty'] = $this->faculty->combo();
        
	// Form validation
        $this->form_validation->set_rules('tname', 'Student Name', 'required');
        $this->form_validation->set_rules('tborn', 'Born Place', 'required');
        $this->form_validation->set_rules('tborndate', 'Born Date', 'required');
        $this->form_validation->set_rules('taddress', 'Address', 'required');
        $this->form_validation->set_rules('tphone', 'Student Phone', 'required|numeric');
        $this->form_validation->set_rules('tnisn', 'NISN', '');
        $this->form_validation->set_rules('tnpsn', 'NPSN', '');
        $this->form_validation->set_rules('tcertificate', 'Certificate No', 'required');
        $this->form_validation->set_rules('tskhun', 'SKHUN', '');
        $this->form_validation->set_rules('tparentname', 'Parents Name', 'required');
        $this->form_validation->set_rules('tparentjob', 'Parents Job', 'required');
        $this->form_validation->set_rules('tparentaddress', 'Parent Address', 'required');
        $this->form_validation->set_rules('tparentphone', 'Parent Phone', 'required|numeric');
        $this->form_validation->set_rules('ttrusteename', 'Guardian Name', 'required');
        $this->form_validation->set_rules('ttrusteejob', 'Guardian Job', 'required');
        $this->form_validation->set_rules('ttrusteeaddress', 'Guardian Address', 'required');
        $this->form_validation->set_rules('ttrusteephone', 'Guardian Phone', 'required');
        
        // registration rules
        $this->form_validation->set_rules('tregid', 'REG-ID', 'required');
        $this->form_validation->set_rules('tregdate', 'Reqistration Date', 'required');
        $this->form_validation->set_rules('cdept', 'Department', 'required');
        $this->form_validation->set_rules('cfaculty', 'Faculty', 'required');
        
        //registration costs
        $this->form_validation->set_rules('tregfee', 'Registration Fee', 'required|numeric');
        $this->form_validation->set_rules('tdevfee', 'Development Fee', 'required|numeric');
        $this->form_validation->set_rules('tschoolfee', 'School Fee', 'required|numeric');
        $this->form_validation->set_rules('tosisfee', 'OSIS Fee', 'required|numeric');
        $this->form_validation->set_rules('tpracticefee', 'Practice Fee', 'required|numeric');
        $this->form_validation->set_rules('totherfee', 'Other Fee', 'required|numeric');
        $this->form_validation->set_rules('ttotal', 'Total Cost', 'required|numeric|callback_valid_nol');
        
        if ($this->form_validation->run($this) == TRUE)
        {   
            
           // student
           $this->student->add($this->input->post('cdept'), $this->input->post('cfaculty'), $this->input->post('tname'), 
                               $this->input->post('tborn'), $this->input->post('tborndate'), $this->input->post('rgenre'),
                               $this->input->post('taddress'), $this->input->post('tphone'), $this->input->post('creligion'),
                               $this->input->post('tnisn'), $this->input->post('tnpsn'), $this->input->post('tcertificate'), 
                               $this->input->post('tskhun'), $this->input->post('tparentname'), $this->input->post('tparentjob'),
                               $this->input->post('tparentaddress'), $this->input->post('tparentphone'),
                               $this->input->post('ttrusteename'), $this->input->post('ttrusteejob'), $this->input->post('ttrusteeaddress'),
                               $this->input->post('ttrusteephone')); 
            
            // registration
            $this->model->no               = $this->input->post('tregid');
            $this->model->dept_id          = $this->input->post('cdept');
            $this->model->student_id       = $this->student->get_max_id();
            $this->model->dates            = $this->input->post('tregdate');
            $this->model->register         = $this->input->post('tregfee');
            $this->model->development      = $this->input->post('tdevfee');
            $this->model->school           = $this->input->post('tschoolfee');
            $this->model->osis             = $this->input->post('tosisfee');
            $this->model->practice         = $this->input->post('tpracticefee');
            $this->model->others           = $this->input->post('totherfee');
            $this->model->total            = $this->input->post('ttotal');
            $this->model->p1               = $this->input->post('tp1');
            $this->model->p2               = $this->input->post('tp2');
            $this->model->financial_year   = $this->financial->get();
            $this->model->notes            = $this->input->post('tnotes');
            $this->model->payment_type     = $this->input->post('cpayment');
            $this->model->log              = $this->session->userdata('log');
            
            // student
             
////            
            $this->model->save();
            $this->session->set_flashdata('message', "One $this->title data successfully saved!");
//            redirect($this->title.'/add');
            echo 'true';
        }
        else
        { 
            //$this->load->view('payroll_form', $data); 
            echo validation_errors();
        }

    }
    
    function delete($uid,$po)
    {
        $this->acl->otentikasi_admin($this->title);
        $sales = $this->model->where('id', $uid)->get();

        if ( $this->valid_period($sales->dates) == TRUE )
        {
            $this->journalgl->remove_journal('TJ', $po); // journal gl
            $this->update_payment_status($po);
            
            $tt = new Tuitiontrans();
            $tt->where('registration', $po)->get();
            $tt->delete_all();
            $this->model->delete();

            $this->session->set_flashdata('message', "1 $this->title successfully removed..!");
        }
        else{ $this->session->set_flashdata('message', "1 $this->title can't removed, invalid period..!"); }

        redirect($this->title);
    }
    
    private function update_payment_status($po)
    {
        $tt = new Tuitiontrans();
        $transs = $tt->where('registration', $po)->get();
                
        $year = new Financial_lib();
        $ps = new Payment_status_lib();
        
        foreach ($transs as $res){ $ps->remove($res->student,$year->get(),$res->month);  }
    }

// ===================================== PRINT ===========================================
    
   function invoice($po=null)
   {
        $this->acl->otentikasi2($this->title);

        $data['title'] = $this->properti['name'].' | Invoice '.ucwords($this->modul['title']);
        $data['h2title'] = 'Print Invoice'.$this->modul['title'];

        $atts = array('width'=> '800','height'=> '600',
                      'scrollbars' => 'yes','status'=> 'yes',
                      'resizable'=> 'yes','screenx'=> '0','screenx' => '\'+((parseInt(screen.width) - 800)/2)+\'',
                      'screeny'=> '0','class'=> 'tombolprint','title'=> 'print', 'screeny' => '\'+((parseInt(screen.height) - 600)/2)+\'');

        $tmpl = array('table_open' => '<table cellpadding="2" cellspacing="1" class="tablemaster">');

        $this->table->set_template($tmpl);
        $this->table->set_empty("&nbsp;");

        $data['pono'] = $po;
        $this->load->view('registration_invoice_form', $data);
   }

   function recap($po=0,$type=null)
   {
       $this->acl->otentikasi2($this->title);

       $data['h2title'] = 'Print Tax Invoice'.$this->modul['title'];

       // SMP
       $smp0 = $this->tm->total($po,6,0);
       $data['smp_spp_piutang'] = intval($smp0['school_fee']+$smp0['cost']);
       $data['smp_osis_piutang'] = intval($smp0['osis']);
       $data['smp_com_piutang'] = intval($smp0['computer']);
       $data['smp_praktek_piutang'] = intval($smp0['practical']);
       $data['smp_bantuan_piutang'] = intval($smp0['aid_foundation']);
       $data['smp_total_piutang'] = $smp0['school_fee']+$smp0['cost']+$smp0['osis']+$smp0['computer']+$smp0['practical'];

       $smp1 = $this->tm->total($po,6,1);
       $data['smp_spp_berjalan'] = intval($smp1['school_fee']+$smp1['cost']);
       $data['smp_osis_berjalan'] = intval($smp1['osis']);
       $data['smp_com_berjalan'] = intval($smp1['computer']);
       $data['smp_praktek_berjalan'] = intval($smp1['practical']);
       $data['smp_bantuan_berjalan'] = intval($smp1['aid_foundation']);
       $data['smp_total_berjalan'] = $smp1['school_fee']+$smp1['cost']+$smp1['osis']+$smp1['computer']+$smp1['practical'];
       
       $smp2 = $this->tm->total($po,6,2);
       $data['smp_spp_depan'] = intval($smp2['school_fee']+$smp2['cost']);
       $data['smp_osis_depan'] = intval($smp2['osis']);
       $data['smp_com_depan'] = intval($smp2['computer']);
       $data['smp_praktek_depan'] = intval($smp2['practical']);
       $data['smp_bantuan_depan'] = intval($smp2['aid_foundation']);
       $data['smp_total_depan'] = $smp2['school_fee']+$smp2['cost']+$smp2['osis']+$smp2['computer']+$smp2['practical'];
       // SMP
       
       // SMA
       $sma0 = $this->tm->total($po,3,0);
       $data['sma_spp_piutang'] = intval($sma0['school_fee']+$sma0['cost']);
       $data['sma_osis_piutang'] = intval($sma0['osis']);
       $data['sma_com_piutang'] = intval($sma0['computer']);
       $data['sma_praktek_piutang'] = intval($sma0['practical']);
       $data['sma_bantuan_piutang'] = intval($sma0['aid_foundation']);
       $data['sma_total_piutang'] = $sma0['school_fee']+$sma0['cost']+$sma0['osis']+$sma0['computer']+$sma0['practical'];
       
       $sma1 = $this->tm->total($po,3,1);
       $data['sma_spp_berjalan'] = intval($sma1['school_fee']+$sma1['cost']);
       $data['sma_osis_berjalan'] = intval($sma1['osis']);
       $data['sma_com_berjalan'] = intval($sma1['computer']);
       $data['sma_praktek_berjalan'] = intval($sma1['practical']);
       $data['sma_bantuan_berjalan'] = intval($sma1['aid_foundation']);
       $data['sma_total_berjalan'] = $sma1['school_fee']+$sma1['cost']+$sma1['osis']+$sma1['computer']+$sma1['practical'];
       
       $sma2 = $this->tm->total($po,3,2);
       $data['sma_spp_depan'] = intval($sma2['school_fee']+$sma2['cost']);
       $data['sma_osis_depan'] = intval($sma2['osis']);
       $data['sma_com_depan'] = intval($sma2['computer']);
       $data['sma_praktek_depan'] = intval($sma2['practical']);
       $data['sma_bantuan_depan'] = intval($sma2['aid_foundation']);
       $data['sma_total_depan'] = $sma2['school_fee']+$sma2['cost']+$sma2['osis']+$sma2['computer']+$sma2['practical'];
       // SMA
       
       
       // STM
       $stm0 = $this->tm->total($po,4,0);
       $data['stm_spp_piutang'] = intval($stm0['school_fee']+$stm0['cost']);
       $data['stm_osis_piutang'] = intval($stm0['osis']);
       $data['stm_com_piutang'] = intval($stm0['computer']);
       $data['stm_praktek_piutang'] = intval($stm0['practical']);
       $data['stm_bantuan_piutang'] = intval($stm0['aid_foundation']);
       $data['stm_total_piutang'] = $stm0['school_fee']+$stm0['cost']+$stm0['osis']+$stm0['computer']+$stm0['practical'];
       
       $stm1 = $this->tm->total($po,4,1);
       $data['stm_spp_berjalan'] = intval($stm1['school_fee']+$stm1['cost']);
       $data['stm_osis_berjalan'] = intval($stm1['osis']);
       $data['stm_com_berjalan'] = intval($stm1['computer']);
       $data['stm_praktek_berjalan'] = intval($stm1['practical']);
       $data['stm_bantuan_berjalan'] = intval($stm1['aid_foundation']);
       $data['stm_total_berjalan'] = $stm1['school_fee']+$stm1['cost']+$stm1['osis']+$stm1['computer']+$stm1['practical'];
       
       $stm2 = $this->tm->total($po,4,2);
       $data['stm_spp_depan'] = intval($stm2['school_fee']+$stm2['cost']);
       $data['stm_osis_depan'] = intval($stm2['osis']);
       $data['stm_com_depan'] = intval($stm2['computer']);
       $data['stm_praktek_depan'] = intval($stm2['practical']);
       $data['stm_bantuan_depan'] = intval($stm2['aid_foundation']);
       $data['stm_total_depan'] = $stm2['school_fee']+$stm2['cost']+$stm2['osis']+$stm2['computer']+$stm2['practical'];
       // STM
       
       // SMEA
       $smea0 = $this->tm->total($po,5,0);
       $data['smea_spp_piutang'] = intval($smea0['school_fee']+$smea0['cost']);
       $data['smea_osis_piutang'] = intval($smea0['osis']);
       $data['smea_com_piutang'] = intval($smea0['computer']);
       $data['smea_praktek_piutang'] = intval($smea0['practical']);
       $data['smea_bantuan_piutang'] = intval($smea0['aid_foundation']);
       $data['smea_total_piutang'] = $smea0['school_fee']+$smea0['cost']+$smea0['osis']+$smea0['computer']+$smea0['practical'];
       
       $smea1 = $this->tm->total($po,5,1);
       $data['smea_spp_berjalan'] = intval($smea1['school_fee']+$smea1['cost']);
       $data['smea_osis_berjalan'] = intval($smea1['osis']);
       $data['smea_com_berjalan'] = intval($smea1['computer']);
       $data['smea_praktek_berjalan'] = intval($smea1['practical']);
       $data['smea_bantuan_berjalan'] = intval($smea1['aid_foundation']);
       $data['smea_total_berjalan'] = $smea1['school_fee']+$smea1['cost']+$smea1['osis']+$smea1['computer']+$smea1['practical'];
       
       $smea2 = $this->tm->total($po,5,2);
       $data['smea_spp_depan'] = intval($smea2['school_fee']+$smea2['cost']);
       $data['smea_osis_depan'] = intval($smea2['osis']);
       $data['smea_com_depan'] = intval($smea2['computer']);
       $data['smea_praktek_depan'] = intval($smea2['practical']);
       $data['smea_bantuan_depan'] = intval($smea2['aid_foundation']);
       $data['smea_total_depan'] = $smea2['school_fee']+$smea2['cost']+$smea2['osis']+$smea2['computer']+$smea2['practical'];
       // SMEA
       
//     -----------------------------------------------------------------------------------------
       $sales = $this->model->where('no', $po)->get();
       $data['total'] = intval($sales->total);
       
       //keterangan
       $data['pono'] = $po;
       $data['dates'] = tglincomplete($sales->dates);
       $data['user'] = '';
       $data['cur'] = $this->currency->get_code($sales->currency);
       $data['log'] = $this->session->userdata('log');

       // property display
       $data['logo'] = $this->properti['logo'];
       $data['paddress'] = $this->properti['address'];
       $data['p_phone1'] = $this->properti['phone1'];
       $data['p_phone2'] = $this->properti['phone2'];
       $data['p_city'] = ucfirst($this->properti['city']);
       $data['p_zip'] = $this->properti['zip'];
       $data['p_npwp'] = $this->properti['npwp'];
       $data['pname'] = $this->properti['name'];

//       if ($type){ $this->load->view('sales_invoice_blank', $data); } else { $this->load->view('sales_order_invoice', $data); }
       
       $this->load->view('invoice', $data);
       
   }

   private function get_romawi($val)
   {
       switch ($val)
       {
           case 01: $val = 'I'; break;
           case 02: $val = 'II'; break;
           case 03: $val = 'III'; break;
           case 04: $val = 'IV'; break;
           case 05: $val = 'V'; break;
           case 06: $val = 'VI'; break;
           case 07: $val = 'VII'; break;
           case 08: $val = 'VIII'; break;
           case 09: $val = 'IX'; break;
           case 10: $val = 'X'; break;
           case 11: $val = 'XI'; break;
           case 12: $val = 'XII'; break;
       }
       return $val;
   }

// ===================================== PRINT ===========================================

// ====================================== REPORT =========================================

    function report()
    {
        $this->acl->otentikasi2($this->title);

        $data['title'] = $this->properti['name'].' | Administrator Report '.ucwords($this->modul['title']);
        $data['h2title'] = 'Report '.$this->modul['title'];
	$data['form_action'] = site_url($this->title.'/report_process');
        $data['link'] = array('link_back' => anchor('sales/','<span>back</span>', array('class' => 'back')));

        $data['currency'] = $this->currency->combo();
        $data['dept']     = $this->dept->combo_all();
        
        $this->load->view('registration_report_panel', $data);
    }

    function report_process()
    {
        $this->acl->otentikasi2($this->title);
        $data['title'] = $this->properti['name'].' | Report '.ucwords($this->modul['title']);

        $cur = $this->input->post('ccurrency');
        $start = $this->input->post('tstart');
        $end = $this->input->post('tend');
        $type = $this->input->post('ctype');
        $status = $this->input->post('cstatus');
        $dept = $this->input->post('cdept');

        $data['currency'] = $cur;
        $data['start'] = $start;
        $data['end'] = $end;
        $data['status'] = $status;
        $data['rundate'] = tgleng(date('Y-m-d'));
        $data['log'] = $this->session->userdata('log');
        $data['dept'] = $this->dept->get_name($dept);

//        Property Details
        $data['company'] = $this->properti['name'];

        $data['registrations'] = $this->tm->report($cur,$dept,$start,$end,$status)->result();
        $total = $this->tm->total_report($cur,$dept,$start,$end,$status);
        
        $data['school_total']    = $total['school_fee'];
        $data['practical_total'] = $total['practical'];
        $data['osis_total']      = $total['osis'];
        $data['computer_total']  = $total['computer'];
        $data['cost_total']      = $total['cost'];
        $data['aid_total']       = $total['aid_foundation'];
        $data['bos_total']       = $total['aid_goverment'];
        $data['balance_total']   = $total['amount'];


        if ($type==0){ $this->load->view('registration_report', $data); }
        elseif ($type==1){ $this->load->view('registration_report_details', $data); }
        elseif ($type==2) {  $this->load->view('registration_recap', $data); }
        
    }


// ====================================== REPORT =========================================
    
    public function valid_nol($val)
    {
        if ($val <= 0)
        {
            $this->form_validation->set_message('valid_nol', "Invalid Total...!");
            return FALSE;
        }
        else { return TRUE; }
    }
    
    public function valid_period($date=null)
    {
        $p = new Period();
        $p->get();

        $month = date('n', strtotime($date));
        $year  = date('Y', strtotime($date));

        if ( intval($p->month) != intval($month) || intval($p->year) != intval($year) )
        {
            $this->form_validation->set_message('valid_period', "Invalid Period.!");
            return FALSE;
        }
        else {  return TRUE; }
    }


}

?>